package com.dhy.commadstate.util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;


public abstract class IBaseAdapter<T> extends BaseAdapter {
    protected List<T> list;
    protected Context context;
    public LayoutInflater inflater;

    public IBaseAdapter(Context context, List<T> list) {
        this.context = context;
        this.list = list != null ? list : new ArrayList<T>();
        inflater = LayoutInflater.from(context);
    }

    public List<T> getList() {
        return list;
    }

    public void notifyDataSetChanged(List<T> list) {
        if (this.list != null) {
            this.list.clear();
            if (list != null) this.list.addAll(list);
        } else {
            this.list = list;
        }
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public T getItem(int i) {
        try {
            return list.get(i);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    protected View convertView;

    public View findViewById(int id) {
        return convertView.findViewById(id);
    }

    public TextView setText(int viewID, int text) {
        return setText(viewID, String.valueOf(text));
    }

    public TextView setText(int viewID, String text) {
        TextView t = (TextView) findViewById(viewID);
        t.setText(text);
        return t;
    }

    public View getView(int position, View convertView, ViewGroup parent, int layoutID) {
        if (convertView == null) {
            convertView = inflater.inflate(layoutID, parent, false);
        }
        this.convertView = convertView;
        convertView.setTag(position);
        return convertView;
    }
}
